//index.js
//获取应用实例
var app = getApp();
Page({
  data: {  
    navbar: ['逛附近', '看推荐'], 
    currentTab: 0,
    fuJinData:[],
    imageses:['1','2','3'],
    lunbotu:[],
    shangPinData1:[],
    shangPinData2:[],
    index:0,
    isShuaXin:0,
      pageNum:1,
      searchword:'',
      pingLunContent:'',
      pingLunData:{isShowPingLun:0,value:'',isshuru:0,yifa:0},
  },
  navbarTap: function(e){  
    this.setData({  
      currentTab: e.currentTarget.dataset.idx ,
      pageNum:1,
        fuJinData:[],
        shangPinData1:[],
        shangPinData2:[],
    })
      this.getDataList();
  },
  showinfo:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:'/pages/index/xianzhiinfo?id='+id
    })
  },
    bindinput:function(e){
      this.setData({
          searchword:e.detail.value
      })
    },
    bindconfirm:function(e){
        this.setData({
            searchword:e.detail.value
        })
        this.search();
    },

    search:function(e){
      // console.log('search')
        var that = this;
        that.setData({
            fuJinData:[],
            shangPinData1:[],
            shangPinData2:[],
            pageNum:1
        })
        this.getDataList();
    },
  onLoad:function(e){
    app.editTabBar();
    // this.getDataList();
      this.getLunBoList();
  },
  onShow:function(e){
    // wx.request({
    //   url: app.data.server+'/index.php/Tongcheng/Book/getUnuseList',
    //   success:function(d){
    //     console.log(d.data);
    //   }
    // })
    //   console.log('12313123')
      this.setData({
          pageNum:1,
          fuJinData:[],
          shangPinData1:[],
          shangPinData2:[]
      })
      this.getDataList();
  },
  onShareAppMessage: function (res) {
    // if (res.from === 'button') {
    //   // 来自页面内转发按钮
    //   console.log(res.target)
    // }
      var id = res.target.dataset.id;
    return {
      title: '分享',
      path: '/pages/index/xianzhiinfo?isShare=1&id='+id
    }
  },
  showShareMenu:function(e){
    // console.log('showShareMenu')
      onShareAppMessage()
  },
    onReachBottom: function (e) {
        // var that = this;
        // // 显示加载图标
        // wx.showLoading({
        //     title: '玩命加载中',
        // })
        // that.setData({
        //     pageNum:1
        // })
        // this.getDataList();
    },
    onPullDownRefresh:function(e){
      var that = this;
        // console.log('下拉刷新')
        // var that = this;
        // that.setData({
        //     pageNum:1,
        //     fuJinData:[]
        // })
        console.log('onPullDownRefresh')
        wx.showLoading({
            title: '玩命加载中',
        })
        that.setData({
            pageNum:that.data.pageNum+1
        })
        this.getDataList();
    },
    //获取闲置数据
    getDataList:function(e){
        var that = this;
        // console.log('1212123123123'+that.data.searchword)
        wx.request({
            url: app.data.server+'/index.php/Tongcheng/Book/getUnuseList?pageNum='+that.data.pageNum+'&currentTab='+that.data.currentTab+'&uid='+getApp().globalData.userInfo.uid+"&keyword="+that.data.searchword+"&longitude="+getApp().globalData.address.longitude+"&latitude="+getApp().globalData.address.latitude,
            success:function(d){
                // console.log('123'+JSON.stringify(d.data));
                wx.hideLoading();
                if(0 == d.data){
                  return;
                }
                //推荐
                if(1 == that.data.currentTab){
                  that.setData({
                      shangPinData1:that.data.shangPinData1.concat(d.data.newList1),
                      shangPinData2:that.data.shangPinData2.concat(d.data.newList2),
                      fuJinData:[]
                  })
                }else{
                    that.setData({
                        fuJinData:that.data.fuJinData.concat(d.data),
                        shangPinData1:[],
                        shangPinData2:[]
                    })
                }
            }
        })
    },
    addShouCang:function(e){
        var that = this;
        var formdata = {
            otherid:e.currentTarget.dataset.id,
            from:2
        }
        app.addShouCang(formdata)
        setTimeout(function(){
            that.setData({
                isshoucang:1
            })
            // wx.startPullDownRefresh()
            that.setData({
                pageNum:1,
                fuJinData:[],
                shangPinData1:[],
                shangPinData2:[]
            })
            that.getDataList();
        },1000)
    } ,
    hidePingLun:function(){
        this.setData({
            pingLunData:{isShowPingLun:0}
        })
    },
    showPingLun:function(e){
        this.setData({
            pingLunData:{isShowPingLun:1,yifa:0},
            id:e.currentTarget.dataset.id,
            from:e.currentTarget.dataset.from
        })
    },
    pingluninput:function(e){
        var that = this;
        this.setData({
            pingLunContent:e.detail.value
        })
    },
    fasong:function(e){
      this.setData({
          "pingLunData.yifa":1
      })
        var that = this;
        var formdata = {
            pingLunContent:this.data.pingLunContent,
            uid:getApp().globalData.userInfo.uid,
            otherid:this.data.id,
            from:this.data.from
        }
        // console.log(JSON.stringify(formdata))
        app.pinglun(formdata);
        setTimeout(function(){
            that.hidePingLun();
            that.setData({
                fuJinData:[
                ],
                pageNum:1,
            });
            that.getDataList();
        },1000)


    },
    getLunBoList:function(e){
      var that = this;
        wx.request({
            url: app.data.server+'/index.php/Tongcheng/Book/getLunboList?type=2',
            success:function(d){
                // console.log('123'+JSON.stringify(d.data));
                that.setData({
                    lunbotu:d.data
                })
            }
        })
    },
    doShuaXin:function(e){
        this.setData({
            pageNum:1,
            fuJinData:[],
            shangPinData1:[],
            shangPinData2:[]
        })
        this.getDataList();
    },
    bindblur:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":0
        })
    },
    bindfocus:function(e){
        var that = this;
        console.log('bindfocus')
        that.setData({
            "pingLunData.isshuru":1
        })
    },
    clearKeyWord:function(e){
        // console.log('clearKeyWord')
        this.setData({
            searchword:'',
            pageNum:1,
            fuJinData:[],
            shangPinData1:[],
            shangPinData2:[]
        })
        this.getDataList();
    }
})
